package org.example.common.exception;

import org.example.common.api.Response;
import org.example.common.api.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常拦截器
 */
@ControllerAdvice
public class GlobalExceptionHandle {

    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public Response<?> handleApiException(ApiException e) {
        return Response.apiFailed(e.getMessage());
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response<?> handleGeneralException(Exception e) {
        if (e.getMessage() == null) {
            return Response.failed();
        }
        return Response.failed(ResultCode.FAILED.getCode(), e.getMessage());
    }
}
